<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js获取当前时间显示在页面上</title>
<script>
/* window.onload=function(){
	//定时器每秒调用一次fnDate()
	setInterval(function(){
		   fnDate();
		   },1000);
	} */
function fnDate(){
	var oDiv=document.getElementById("div1");
	var date=new Date();
	var year=date.getFullYear();//当前年份
	var month=date.getMonth();//当前月份
	var data=date.getDate();//天
	var hours=date.getHours();//小时
	var minute=date.getMinutes();//分
	//var second=date.getSeconds();//秒
	var time=year+"-"+fnW((month+1))+"-"+fnW(data)+" "+fnW(hours)+":"+fnW(minute);
	oDiv.innerHTML=time;
	}
	//补位 当某个字段不是两位数时补0
	function fnW(str){
	var num;
	str>10?num=str:num="0"+str;
	return num;
	} 
	//js 获取当前时间
	function refresh(){
	var oDiv=document.getElementById("div1");
	var date=new Date();
	var year=date.getFullYear();//当前年份
	var month=date.getMonth();//当前月份
	var data=date.getDate();//天
	var hours=date.getHours();//小时
	var minute=date.getMinutes();//分
	/**var second=date.getSeconds();//秒 */
	var time=year+"-"+fnW((month+1))+"-"+fnW(data)+" "+fnW(hours)+":"+fnW(minute);
	oDiv.innerHTML=time;
	}
	 //补位 当某个字段不是两位数时补0
	function fnW(str){
		var num;
		str>=10?num=str:num="0"+str;
		return num;
	} 
	 //刷新时间并置前
	window.onload = function () {
        for (var i = 0; i < document.getElementsByTagName("div").length; i++) {
            document.getElementsByTagName("div1")[i].onclick = function () {
  
                this.style.zIndex = 4;
            }
  
             document.getElementsByTagName("div1")[i].onmouseout = function () {
  
                this.style.zIndex =1;
            }
        }
  
    }
</script>
</head>
<body>
    <div id="div1"> </div>
    <div id="div2"></div>
     <input type="button" onclick="refresh()">
</body>
</html>